#include <iostream>
#include "igslib_iutil.h"
#include "igslib_igpio.h"
#include "igslib_iserial_manager.h"
#include "igslib_iacceptor.h"
#include "igslib_idispenser.h"
#include "igslib_imeter.h"
#include "igslib_imarquee.h"
#include "igslib_ivr.h"
#include "igslib_idip_switch.h"
#include "igslib_ibattery.h"

#include<fstream>
#include<cstring>


#define txlen 8
#define rxlen 8

namespace igslib
{
    namespace project_sample
    {
		struct Upack_t
		{      
			unsigned char startflg;
			int lastPN;
			unsigned char rstFlag;

			Upack_t()
			{
				memset(this,0,sizeof(Upack_t));
				startflg=0xc0;
				lastPN = 1;
				rstFlag= 1;
			}
		};

       				
		static void dragonball_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");	
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);


			std::map<std::string,IMCUUart*> usart = srm->New_ProjMCU_uart("DragonBallRace");	
			usart["CON10"]->SetSPI(spi);
			usart["CON10"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON10"]->SetReceiveLen(8);
			usart["CON10"]->SetReceiveTimeout(10000); //10s
			usart["CON10"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON10"]->SetParityMode(IUart::pm_none);
			usart["CON10"]->SetDataBits(IUart::data_8bits);
			usart["CON10"]->SetStopBits(IUart::stop_1bit);
		    usart["CON10"]->Open(IUart::Auto);

			 
			
			 ICanbus* cbus = srm->New_ProjMCU_canbus("DragonBallRace");
			 cbus->SetSPI(spi);			 
			 cbus->SetID(40);
			 cbus->SetTargetID(41);
			
	
            IExternalBoardManager* ebm = GetExternalBoardManagerInstance();
            IExternalBoard* exb = ebm->New_ProjMCU("DragonBallRace");
            exb->SetSPI(spi);

			
			 IVRManager* vrm = GetVRManagerInstance();
			 IVR* vr = vrm->New_ProjMCU("DragonBallRace");
			 vr->SetSPI(spi);
			
			IGPIOManager* gpm = GetGPIOManagerInstance();
			std::vector<IGPIO*> gpin;
			std::vector<IGPIO*> gpout;
			for(int i=0;i<15;i++) //依據專案定義GPIO output
			{
				IGPIO* gptmp = gpm->New_ProjMCU("DragonBallRace");
				gptmp->SetSPI(spi);
				gptmp->SetMode(IGPIO::mode::output);
				gptmp->SetNumber(i);
				gpout.push_back(gptmp);			
			}
			
			for(int i=0;i<8;i++) //依據專案定義GPIO input
			{
				IGPIO* gptmp = gpm->New_ProjMCU("DragonBallRace");
				gptmp->SetSPI(spi);
				gptmp->SetMode(IGPIO::mode::input);
				gptmp->SetNumber(i);
				gpin.push_back(gptmp);			
			}
			
			
			IMarqueeManager* marqm = GetMarqueeManagerInstance();
			//PWM 
			IMarquee* pwmlamp[3]; 		
			for(int i=0;i<3;i++)
			{
				pwmlamp[i] = marqm->New_ProjMCU("DragonBallRace");
				pwmlamp[i]->SetSPI(spi);
				pwmlamp[i]->SetGPIO(gpout[5+i]);
			}	
			//scrolling text  
			IMarquee* runlamp= marqm->New_ProjMCU("DragonBallRace");
				runlamp->SetSPI(spi);
				runlamp->SetGPIO(gpin[10],gpin[11],NULL);  //6024燈條
				//runlamp->SetGPIO(gpin[10],gpin[11],gpin[12]);  //5026燈條
			
	

            IAcceptorManager* coinm = GetAcceptorManagerInstance();
            IAcceptor* coin = coinm->New_ProjMCU("DragonBallRace");
            coin->SetSPI(spi);
            coin->SetGPIO(gpin[1],NULL);
            coin->SetConfirmRange(10,150);
            coin->SetJammedTime(160);



            IDispenserManager* dism = GetDispenserManagerInstance();
            IDispenser* ticket = dism->New_ProjMCU("DragonBallRace");
            ticket->SetSPI(spi);
            ticket->SetConfirmRange(15,1000);
            ticket->SetEmptyTime(1000);
            ticket->SetJammedTime(1000);
            ticket->SetHighActive(false);
            ticket->SetGPIO(gpin[7],gpout[4],NULL);	




            IMeterManager* mtm=GetMeterManagerInstance();
            IGPIOMeter* mt_coin = mtm->New_ProjGPIOMeter("DragonBallRace");		
            mt_coin->SetSPI(spi);
            mt_coin->SetDutyRange(50,450);
            mt_coin->SetGPIO(gpout[8]);

            IGPIOMeter* mt_ticket = mtm->New_ProjGPIOMeter("DragonBallRace");
            mt_ticket->SetSPI(spi);
            mt_ticket->SetDutyRange(50,450);
            mt_ticket->SetGPIO(gpout[9]);
			
			
			 // IDipSwitchManager* dip8m=GetDipSwitchManagerInstance();
			 // IDipSwitch* dip8 = dip8m->New_ProjMCU("DragonBallRace");		  
             // dip8->SetSPI(spi);
   

			IBatteryManager* batm=GetBatteryManagerInstance();
			IBattery* bat = batm->New_ProjMCU("DragonBallRace");	
            bat->SetSPI(spi);
			
			
			

            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;

            int ikv_Request_Action=0;		
            IProtectUnit* ikv=exb->GetProtectUnit();

			int usart_idx=0;
			char usart_txbuf[40]={0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48};
			char usart_rxbuf[40]={0};
			

            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();
                }

                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
                    time_idx++;


                    for(auto each:gpin)
                    {					

                        if(each->GetState() == IGPIO::state::high)
                            std::cout<<"Input"<< each->GetNumber()<<": High  ";
                        else
                            std::cout<<"Input"<< each->GetNumber()<<": Low   ";		
                    }
                    std::cout<<std::endl;	

                    CoinVal+=coin->GetValue();
                    std::cout<<"CoinVal: "<< CoinVal<<std::endl;

                    TicketVal=ticket->GetValue();
                    std::cout<<"TicketVal: "<< TicketVal<<std::endl;
				  
				    // std::string strdip=dip8->GetStateString();
					 // std::cout<<"DipSwitch: "<<strdip<<std::endl;

					 auto strtn=ticket->GetState();
					 if(strtn == IDispenser::State::error)
					 std::cout<<"ticket Error"<<std::endl;	
					 
                     {
                         float value = 0;
                         int result = vr->GetPosition(&value);
                         if (result == -1)
                         {
                             igslib::util::Log("%s> GetPosition() fail, result=%g\n",__func__,result);
                         }
                         std::cout<<"VR absolute:"<<value<<std::endl;	 
                     }

                     {
                         float value = 0;
                         int result = vr->GetOffset(&value);
                         if (result == -1)
                         {
                             igslib::util::Log("%s> GetPosition() fail, result=%g\n",__func__,result);
                         }
                         std::cout<<"VR relative:"<<value<<std::endl;   
                     }
						
					 
					 
					if(time_idx==4)
					{
						act_cnt=0;
	
						for(auto each:gpout)
						{					
							if(act_cnt%2)
							each->SetState(IGPIO::state::low);
							else
							each->SetState(IGPIO::state::high);
							
							act_cnt++;
						}
						
						mt_coin->AddValue(3);
						
					}
					else if(time_idx==8)
					{
						act_cnt=0;		
						for(auto each:gpout)
						{					
							if(act_cnt%2)
							each->SetState(IGPIO::state::high);
							else
							each->SetState(IGPIO::state::low);							
							act_cnt++;
						}
						
						if(ticket->GetState()==IDispenser::State::idle)
						{
							ticket->SetValue(3);
							TicketVal=ticket->GetValue();
							std::cout<<"TicketVal: "<< TicketVal<<std::endl;
							ticket->PrizeOut();
						}
						
						mt_ticket->AddValue(3);					
						time_idx=0;
					}
					
					if(usart_idx==10)
					{
						
						int trans_cnt=0;
						auto resat=usart["CON10"]->GetState();
						if(resat==IMCUUart::UState::Error)
						 std::cout<<"USART ERROR"<<std::endl;
						
						
						if(resat==IMCUUart::UState::DValid)	
						{
							int rlen=usart["CON10"]->Receive(usart_rxbuf,rxlen);
							usart["CON10"]->Send(usart_txbuf,txlen);
							std::cout<<"USART usart_rxbuf count:"<<trans_cnt<<" ";
							for(auto& each:usart_rxbuf)
								std::cout << each<<"  "; 
							std::cout<< std::endl;
							std::cout<<"USART usart_txbuf count:"<<trans_cnt<<" ";
							for(auto& each:usart_txbuf)
								std::cout << each<<"  "; 
							std::cout<< std::endl;
							
							for (int i=0;i<8;i++)
							usart_txbuf[i]=trans_cnt;
							trans_cnt++;
						}
						else if(resat==IMCUUart::UState::Transferring)	
							std::cout<<"USART Transferring count: "<<trans_cnt<<std::endl;
					
						usart_idx=0;
					}
					 usart_idx++;
					
					//0%~100%
					if(lamptime_idx==4)
					{
						pwmlamp[0]->SetDutyCycle(100);
						pwmlamp[1]->SetDutyCycle(0);
						pwmlamp[2]->SetDutyCycle(0);
						runlamp->RunGroup(1,false);
						runlamp->SetPatternDuration(10);	
					}
					else if(lamptime_idx==8)
					{	
						pwmlamp[0]->SetDutyCycle(0);
						pwmlamp[1]->SetDutyCycle(100);
						pwmlamp[2]->SetDutyCycle(0);
						runlamp->RunGroup(0,false);
					}
					else if(lamptime_idx==12)
					{
						pwmlamp[0]->SetDutyCycle(0);
						pwmlamp[1]->SetDutyCycle(0);
						pwmlamp[2]->SetDutyCycle(100);
						runlamp->RunGroup(2,false);
						runlamp->SetPatternDuration(0);					
					}
					else if(lamptime_idx==16)
					{
						pwmlamp[0]->SetDutyCycle(100);
						pwmlamp[1]->SetDutyCycle(100);
						pwmlamp[2]->SetDutyCycle(100);
						runlamp->RunGroup(0,false);						
						lamptime_idx=0;
					}
					lamptime_idx++;
					
				
                }
            }

            exit(EXIT_SUCCESS);
        }	


		
		static void dragonball_marquee_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);
				
			IGPIOManager* gpm = GetGPIOManagerInstance();
			std::vector<IGPIO*> gpout;
			for(int i=0;i<15;i++) //依據專案定義GPIO output
			{
				IGPIO* gptmp = gpm->New_ProjMCU("DragonBallRace");
				gptmp->SetSPI(spi);
				gptmp->SetMode(IGPIO::mode::output);
				gptmp->SetNumber(i);
				gpout.push_back(gptmp);			
			}
				
			IMarqueeManager* marqm = GetMarqueeManagerInstance();
			//scrolling text  
			IMarquee* runlamp= marqm->New_ProjMCU("DragonBallRace");
			runlamp->SetSPI(spi);
			runlamp->SetGPIO(gpout[10],gpout[11],NULL);  //6024燈條 龜龜燈條			
	
            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;
			int usart_idx=0;

			/**************************
			*  make pattern & load pattern
			*******************************/
			
			char LED_number = 7;
			igslib::LEDColor ALEDoff[] ={0x00,0x00,0x00 ,0x00,0x00,0x00 ,0x00,0x00,0x00 ,0x00,0x00,0x00 ,0x00,0x00,0x00 ,0x00,0x00,0x00 ,0x00,0x00,0x00};
			igslib::LEDColor ALEDon[]  ={0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF};
			igslib::LEDColor LEDcolor[] ={0xFF,0x00,0x00 ,0x00,0xFF,0x00 ,0x00,0x00,0xFF ,0xFF,0xFF,0x00 ,0x00,0xFF,0xFF ,0xFF,0x00,0xFF ,0xFF,0xFF,0xFF};
			igslib::LEDColor LEDBlue[] ={0xFF,0x00,0x00 ,0xFF,0x00,0x00 ,0xFF,0x00,0x00 ,0xFF,0x00,0x00 ,0xFF,0x00,0x00 ,0xFF,0x00,0x00 ,0xFF,0x00,0x00};
			igslib::LEDColor LEDGreen[] ={0x00,0x00,0xFF ,0x00,0x00,0xFF ,0x00,0x00,0xFF ,0x00,0x00,0xFF ,0x00,0x00,0xFF ,0x00,0x00,0xFF ,0x00,0x00,0xFF};
	
			unsigned char group0[1]={3}; //全blue
			unsigned char group1[2]={0,1}; //閃白
			unsigned char group2[2]={0,2}; //閃彩色
			unsigned char group3[3]={2,3,4}; 
  
  
            runlamp->ImportPattern(0,LED_number,ALEDoff);
			runlamp->ImportPattern(1,LED_number,ALEDon);
			runlamp->ImportPattern(2,LED_number,LEDcolor);
			runlamp->ImportPattern(3,LED_number,LEDBlue);
			runlamp->ImportPattern(4,LED_number,LEDGreen);
			
			
			runlamp->ImportGroup(0,sizeof(group0),group0);
			runlamp->ImportGroup(1,sizeof(group1),group1);
			runlamp->ImportGroup(2,sizeof(group2),group2);
			runlamp->ImportGroup(3,sizeof(group2),group3);
			
		
					
            while(1)
            {			
				d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();	
				}
				
				d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
					
					if(lamptime_idx==4)
					{

						runlamp->RunGroup(0,false);
						runlamp->SetPatternDuration(2);	
					}
					else if(lamptime_idx==8)
					{	

						runlamp->RunGroup(1,false);
						runlamp->SetPatternDuration(20);									
					}
					else if(lamptime_idx==12)
					{	

						runlamp->RunGroup(2,false);
						runlamp->SetPatternDuration(20);									
					}
					else if(lamptime_idx==16)
					{

						runlamp->RunGroup(3,false);
						runlamp->SetPatternDuration(100);	
						lamptime_idx=0;						
					}
					lamptime_idx++;
                }
		
            }

           exit(EXIT_SUCCESS);
        }	
		
		
		static void dragonball_pwm_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);
				
			IGPIOManager* gpm = GetGPIOManagerInstance();
			std::vector<IGPIO*> gpout;
			for(int i=0;i<15;i++) //依據專案定義GPIO output
			{
				IGPIO* gptmp = gpm->New_ProjMCU("DragonBallRace");
				gptmp->SetSPI(spi);
				gptmp->SetMode(IGPIO::mode::output);
				gptmp->SetNumber(i);
				gpout.push_back(gptmp);			
			}
				
			IMarqueeManager* marqm = GetMarqueeManagerInstance();
			//PWM 
			IMarquee* pwmlamp[3]; 		
			for(int i=0;i<3;i++)
			{
				pwmlamp[i] = marqm->New_ProjMCU("DragonBallRace");
				pwmlamp[i]->SetSPI(spi);
				pwmlamp[i]->SetGPIO(gpout[5+i]);
			}	

	
            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;
			int usart_idx=0;

					
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();	
				}
				
				d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
                    time_idx++;
			
					//0%~100%
					if(lamptime_idx==4)
					{
						pwmlamp[0]->SetDutyCycle(100);
						pwmlamp[1]->SetDutyCycle(0);
						pwmlamp[2]->SetDutyCycle(0);
					}
					else if(lamptime_idx==8)
					{	
						pwmlamp[0]->SetDutyCycle(0);
						pwmlamp[1]->SetDutyCycle(100);
						pwmlamp[2]->SetDutyCycle(0);
					}
					else if(lamptime_idx==12)
					{
						pwmlamp[0]->SetDutyCycle(0);
						pwmlamp[1]->SetDutyCycle(0);
						pwmlamp[2]->SetDutyCycle(100);
					}
					 else if(lamptime_idx==16)
					 {
						 pwmlamp[0]->SetDutyCycle(100);
						 pwmlamp[1]->SetDutyCycle(100);
						 pwmlamp[2]->SetDutyCycle(100);				
						 lamptime_idx=0;
					 }
					lamptime_idx++;
                }
		
            }

           exit(EXIT_SUCCESS);
        }	
			
		static void dragonball_dipswitch_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);
				
		
			IDipSwitchManager* dip8m=GetDipSwitchManagerInstance();				
			IDipSwitch* dip8 = dip8m->New_ProjMCU("DragonBallRace");	
            dip8->SetSPI(spi);
		

            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;
			int usart_idx=0;

					
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();	
				}
				
				
				d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 1000) //1s printf
                {
                    d2=util::GetTicks();
					
				   int rtn=dip8->GetPinStateValue();	
				   std::cout<<"DipSwitchA: "<<rtn<<std::endl;
				 
				   std::string strdip=dip8->GetStateString();
			       std::cout<<"DipSwitchB: "<<strdip<<std::endl;			
				}
					
				
            }

            exit(EXIT_SUCCESS);
        }

	   static void dragonball_Rotary_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");			
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);
				
		
			IVRManager* vrm = GetVRManagerInstance();
			 IVR* vr = vrm->New_ProjMCU("DragonBallRace");
			 vr->SetSPI(spi);
		

            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;
			int usart_idx=0;

					
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();	
				}
				
				
				d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //1s printf
                {
                    d2=util::GetTicks();
                    float value = 0;
                    int result = vr->GetOffset(&value);
                    if (result == -1)
                    {
                        igslib::util::Log("%s> GetPosition() fail, result=%g\n",__func__,result);
                    }
                    std::cout<<"VR relative:"<<value<<std::endl;   
				}
            }

            exit(EXIT_SUCCESS);
        }
					
		static void dragonball_CANBus_lodding_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");			
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);

			
			ICanbus* cbs=srm->New_ProjMCU_canbus("DragonBallRace");
			cbs->SetSPI(spi);
			cbs->SetID(0);  //host id

            static float rtn = 0;				
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

            unsigned char time_idx=0; 			
			unsigned long long rs232_trans_cnt=0;
			unsigned long long rs485_trans_cnt=0;
			
			char txbuf[8]={0xc0,0x00,0x00,0x00,0x00,0xC0,0x00,0x00 };
			char rxbuf[8]={0};

			spi->Update();		
			util::SleepMSTime(16);		
			spi->Update();				
			util::SleepMSTime(16);
			
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
            				
					d0 = util::GetTicks();
                    spi->Update();	
			       // std::cout<<"USART usart_Rxbuf : ";
					// for(int i=0;i<8;i++)
					// printf("0x%02x ",rs485_rxbuf[i]);
					// std::cout<< std::endl;								
                }
				
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 250) //0.5s printf
                {
                    d2=util::GetTicks();
				
					if(time_idx%2==0)
					{
						cbs->SetTargetID(1); 
						cbs->Send(txbuf,8);					
					}
					else
					{

						 cbs->SetTargetID(1); 
						 cbs->Receive(rxbuf,8);	
						
						std::cout<<"CANBUS Rxbuf : ";
						for(int i=0;i<8;i++)
						printf("0x%02x ",rxbuf[i]);
						std::cout<< std::endl;	
						
					}	
					time_idx++;									
                }
								
            }

            exit(EXIT_SUCCESS);
        }
		static void dragonball_uart_transfer_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");		
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);


			std::map<std::string,IMCUUart*> usart = srm->New_ProjMCU_uart("DragonBallRace");	
			usart["CON10"]->SetSPI(spi);
			usart["CON10"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON10"]->SetReceiveLen(8);
			usart["CON10"]->SetReceiveTimeout(10000); //10s
			usart["CON10"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON10"]->SetParityMode(IUart::pm_none);
			usart["CON10"]->SetDataBits(IUart::data_8bits);
			usart["CON10"]->SetStopBits(IUart::stop_1bit);
		    usart["CON10"]->Open(IUart::Auto);	


			usart["CON3"]->SetSPI(spi);
			usart["CON3"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON3"]->SetReceiveLen(8);
			usart["CON3"]->SetReceiveTimeout(10000); //10s
			usart["CON3"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON3"]->SetParityMode(IUart::pm_none);
			usart["CON3"]->SetDataBits(IUart::data_8bits);
			usart["CON3"]->SetStopBits(IUart::stop_1bit);
		    usart["CON3"]->Open(IUart::Auto);
			

            static float rtn = 0;				
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

            int time_idx=0; 			
			unsigned long long rs232_trans_cnt=0;
			unsigned long long rs485_trans_cnt=0;
			
			char rs232_txbuf[8]={0xc0,0x81,0,0,0,0,0,0};
			char rs232_rxbuf[8]={0};
			char DValid_rs232=1;		
		    Upack_t rs232;
			
			
			char rs485_txbuf[8]={0xc0,0x81,0,0,0,0,0,0};
			char rs485_rxbuf[8]={0};
			char DValid_rs485=1;		
		    Upack_t rs485;
			
					
			spi->Update();		
			util::SleepMSTime(16);		
			spi->Update();				
			util::SleepMSTime(16);
			
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    
					auto rs232sat=usart["CON10"]->GetState();
					if(rs232sat==IMCUUart::UState::Error)
					std::cout<<"RS232 ERROR RS232 ERROR RS232 ERROR"<<std::endl;
					
					auto rs485sat=usart["CON3"]->GetState();
					if(rs485sat==IMCUUart::UState::Error)
					std::cout<<"RS485 ERROR RS485 ERROR RS485 ERROR"<<std::endl;
					
											
					
					if(rs232sat==IMCUUart::UState::DValid  && DValid_rs232==1)	
					{					
					  DValid_rs232=0;
					  rs232_txbuf[0] = rs232.startflg;
					  rs232_txbuf[1] = rs232.lastPN;
					  rs232_txbuf[1] |= (rs232.rstFlag) ? 0x80 : 0;
					  
					  usart["CON10"]->Send(rs232_txbuf,txlen);
					}
					
					
					if(rs485sat==IMCUUart::UState::DValid  && DValid_rs485==1)	
					{					
					  DValid_rs485=0;
					  rs485_txbuf[0] = rs485.startflg;
					  rs485_txbuf[1] = rs485.lastPN;
					  rs485_txbuf[1] |= (rs485.rstFlag) ? 0x80 : 0;
					  
					  usart["CON3"]->Send(rs485_txbuf,txlen);
					}
					
				
	
					 std::cout<<"Rs232 Transfer count: "<<rs232_trans_cnt<<"          Rs485 Transfer count: "<<rs485_trans_cnt<<std::endl;
		
					
					d0 = util::GetTicks();
                    spi->Update();	
					
					int rlen=usart["CON10"]->Receive(rs232_rxbuf,rxlen);					
					if(rs232_rxbuf[0]==0xc1)
					{
						int recvPN= rs232_rxbuf[1]& 0x7f;
						if( recvPN == rs232.lastPN )
						{
							rs232.rstFlag=0;
							
							DValid_rs232=1;
							rs232.lastPN++;
							if(rs232.lastPN > 127)
							rs232.lastPN=1;
							rs232_trans_cnt++;
							
						}
					}
					
					
					usart["CON3"]->Receive(rs485_rxbuf,rxlen);					
					if(rs485_rxbuf[0]==0xc1)
					{
						int recvPN= rs485_rxbuf[1]& 0x7f;
						if( recvPN == rs485.lastPN )
						{
							rs485.rstFlag=0;
							
							DValid_rs485=1;
							rs485.lastPN++;
							if(rs485.lastPN > 127)
							rs485.lastPN=1;
							rs485_trans_cnt++;
							
						}
					}
			       // std::cout<<"USART usart_Rxbuf : ";
					// for(int i=0;i<8;i++)
					// printf("0x%02x ",rs485_rxbuf[i]);
					// std::cout<< std::endl;			
                }
				
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
										
                }
				
				
            }

            exit(EXIT_SUCCESS);
        }	
		
		static void dragonball_uart_receive_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);


			std::map<std::string,IMCUUart*> usart = srm->New_ProjMCU_uart("DragonBallRace");	
			usart["CON10"]->SetSPI(spi);
			usart["CON10"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON10"]->SetReceiveLen(8);
			usart["CON10"]->SetReceiveTimeout(65530); //60s
			usart["CON10"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON10"]->SetParityMode(IUart::pm_none);
			usart["CON10"]->SetDataBits(IUart::data_8bits);
			usart["CON10"]->SetStopBits(IUart::stop_1bit);
			usart["CON10"]->SetSlaveOperation(true); //設定Slave 當外部packetage會直接接收存放置register
		    usart["CON10"]->Open(IUart::Auto);	


			usart["CON3"]->SetSPI(spi);
			usart["CON3"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON3"]->SetReceiveLen(8);
			usart["CON3"]->SetReceiveTimeout(65530); //60S
			usart["CON3"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON3"]->SetParityMode(IUart::pm_none);
			usart["CON3"]->SetDataBits(IUart::data_8bits);
			usart["CON3"]->SetStopBits(IUart::stop_1bit);
			usart["CON3"]->SetSlaveOperation(true); //設定Slave 當外部packetage會直接接收存放置register
		    usart["CON3"]->Open(IUart::Auto);
			

            static float rtn = 0;				
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

            int time_idx=0; 			
			unsigned long long rs232_trans_cnt=0;
			unsigned long long rs485_trans_cnt=0;
			
			char rs232_txbuf[8]={0xc1,0xFF,0,0,0,0,0,0};
			char rs232_rxbuf[8]={0};
			char DValid_rs232=0;		
		    Upack_t rs232;
			
			
			char rs485_txbuf[8]={0xc1,0xFF,0,0,0,0,0,0};
			char rs485_rxbuf[8]={0};
			char DValid_rs485=0;		
		    Upack_t rs485;
			
			rs232.lastPN=-1;
			rs485.lastPN=-1;
					
			spi->Update();		
			util::SleepMSTime(16);		
			spi->Update();				
			util::SleepMSTime(16);
			
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    
					auto rs232sat=usart["CON10"]->GetState();
					if(rs232sat==IMCUUart::UState::Error)
					std::cout<<"RS232 ERROR RS232 ERROR RS232 ERROR"<<std::endl;
					
					auto rs485sat=usart["CON3"]->GetState();
					if(rs485sat==IMCUUart::UState::Error)
					std::cout<<"RS485 ERROR RS485 ERROR RS485 ERROR"<<std::endl;
					
					
					if(rs232sat==IMCUUart::UState::DValid  && DValid_rs232==1)	
					{
						 DValid_rs232=0;
					     rs232_txbuf[0] = 0xC1;
					     rs232_txbuf[1] = rs232.lastPN;
						 usart["CON10"]->Send(rs232_txbuf,8);
					}
					// else if(rs232sat==IMCUUart::UState::DValid) 
					// {					
						// usart["CON10"]->Send(rs232_txbuf,0);	
					// }
					
			
					
				   if(rs485sat==IMCUUart::UState::DValid  && DValid_rs485==1)	
					{
						 DValid_rs485=0;
					     rs485_txbuf[0] = 0xC1;
					     rs485_txbuf[1] = rs485.lastPN;
						 usart["CON3"]->Send(rs485_txbuf,8);
					}
					// else if(rs485sat==IMCUUart::UState::DValid) 
					// {					
						// usart["CON3"]->Send(rs485_txbuf,0);	
					// }
					
															
					std::cout<<"Rs232 Transfer count: "<<rs232_trans_cnt<<"          Rs485 Transfer count: "<<rs485_trans_cnt<<std::endl;
		
					
					d0 = util::GetTicks();
                    spi->Update();	
					
					int rlen=usart["CON10"]->Receive(rs232_rxbuf,rxlen);					
					if(rs232_rxbuf[0]==0xc0)
					{
						int recvPN= rs232_rxbuf[1]& 0x7f;
						if( (recvPN!=0 && rs232.lastPN == -1)  || 

							(recvPN!=0 && (recvPN == rs232.lastPN+1)) ||
							
							(recvPN == 1 && (rs232.lastPN == 127))  )				
						{
							DValid_rs232=1;
							rs232.lastPN=recvPN;
							rs232_trans_cnt++;
						}
					}
					
					
					usart["CON3"]->Receive(rs485_rxbuf,rxlen);					
					if(rs485_rxbuf[0]==0xc0)
					{
					   int recvPN= rs485_rxbuf[1]& 0x7f;
						if( (recvPN!=0 && rs485.lastPN == -1)  || 

							(recvPN!=0 && (recvPN == rs485.lastPN+1)) ||
							
							(recvPN == 1 && (rs485.lastPN == 127))  )				
						{
							DValid_rs485=1;
							rs485.lastPN=recvPN;
							rs485_trans_cnt++;
						}
					}				
				    // std::cout<<"USART usart_Rxbuf : ";
					// for(int i=0;i<8;i++)
					// printf("0x%02x ",rs485_rxbuf[i]);
					// std::cout<< std::endl;
					
					// util::SleepMSTime(2);
                }
				
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
										
                }
				
				
            }

            exit(EXIT_SUCCESS);
        }
		
		static void dragonball_RS485_bridge_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);
			
			std::map<std::string,IMCUUart*> usart = srm->New_ProjMCU_uart("DragonBallRace");
			usart["CON3"]->SetSPI(spi);
			usart["CON3"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON3"]->SetReceiveLen(8);
			usart["CON3"]->SetReceiveTimeout(65530); //60S
			usart["CON3"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON3"]->SetParityMode(IUart::pm_none);
			usart["CON3"]->SetDataBits(IUart::data_8bits);
			usart["CON3"]->SetStopBits(IUart::stop_1bit);
		    usart["CON3"]->Open(IUart::Auto);
			
	
			IUart* uart = srm->NewM01Uart();
            IUart::DeviceNumber dn = IUart::ttyS1;
            uart->SetBaudRate(115200);
            uart->SetDataBits(IUart::data_8bits);
            bool odr = uart->Open(dn); // open device result
            std::cout << "Open device[" << dn << "]: " << odr << std::endl;
			
			

            static float rtn = 0;				
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

            int time_idx=0; 			
			unsigned long long rs232_trans_cnt=0;
			unsigned long long rs485_trans_cnt=0;
			
			int Receive_cnt=0;
			
			char rs232_txbuf[8]={0xc1,0xFF,0,0,0,0,0,0};
			char rs232_rxbuf[8]={0};
			char DValid_rs232=0;		
		    Upack_t rs232;
				
				
			char rs485_txbuf[8]={0xc1,0xFF,0,0,0,0,0,0};
			char rs485_rxbuf[8]={0};
			char DValid_rs485=1;		
		    Upack_t rs485;
			
			rs232.lastPN=-1;
			rs485.lastPN=0x1;
					
			spi->Update();		
			util::SleepMSTime(16);		
			spi->Update();				
			util::SleepMSTime(16);
			
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                   d0 = util::GetTicks(); 

				   
				   auto rs485sat=usart["CON3"]->GetState();
					if(rs485sat==IMCUUart::UState::Error)
					std::cout<<"RS485 ERROR RS485 ERROR RS485 ERROR"<<std::endl;
					
				   if(rs485sat==IMCUUart::UState::DValid  && DValid_rs485==1)	
					{					
					  DValid_rs485=0;
					  rs485_txbuf[0] = rs485.startflg;
					  rs485_txbuf[1] = rs485.lastPN;
					  rs485_txbuf[1] |= (rs485.rstFlag) ? 0x80 : 0;
					  
					  usart["CON3"]->Send(rs485_txbuf,txlen);
					  
		
					  
					}
															
					std::cout<<" Rs485 Transfer count: "<<rs485_trans_cnt<<std::endl;
										
                    spi->Update();	
									
					usart["CON3"]->Receive(rs485_rxbuf,rxlen);					
					if(rs485_rxbuf[0]==0xc1)
					{
						int recvPN= rs485_rxbuf[1]& 0x7f;
						if( recvPN == rs485.lastPN )
						{
							rs485.rstFlag=0;
							
							DValid_rs485=1;
							rs485.lastPN++;
							if(rs485.lastPN > 127)
							rs485.lastPN=1;
							rs485_trans_cnt++;						
							
						}
		
					}	
                }

				
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 100) //0.1s
                {
                    d2=util::GetTicks();
																	
					/***m01 uart **/
					char data[8] = {0};	
					int length = uart->Receive(data,sizeof(data));
					
					if (length > 0)
					{
						for(int i=0;i<length;i++)
						{
							rs232_rxbuf[Receive_cnt++]=data[i];
						}
					}					
					if(Receive_cnt>=8)
					{
						Receive_cnt=0;
						if(rs232_rxbuf[0]==0xc0) //接收正確
						{
						   int recvPN= rs232_rxbuf[1]& 0x7f;
							if( (recvPN!=0 && rs232.lastPN == -1)  || 

								(recvPN!=0 && (recvPN == rs232.lastPN+1)) ||
								
								(recvPN == 1 && (rs232.lastPN == 127))  )				
							{

								rs232.lastPN=recvPN;
														
								//  送出
								rs232_txbuf[0] = 0xc1;
								rs232_txbuf[1] = rs232.lastPN;
								int send_rtn = uart->Send(rs232_txbuf,8);
							}					
						}				
						
					}
											
				}
				
				
            }

            exit(EXIT_SUCCESS);
        }
		
		
		static void dragonball_RS485_RS232_bridge_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);
			
			std::map<std::string,IMCUUart*> usart = srm->New_ProjMCU_uart("DragonBallRace");
			usart["CON3"]->SetSPI(spi);
			usart["CON3"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON3"]->SetReceiveLen(8);
			usart["CON3"]->SetReceiveTimeout(65530); //60S
			usart["CON3"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON3"]->SetParityMode(IUart::pm_none);
			usart["CON3"]->SetDataBits(IUart::data_8bits);
			usart["CON3"]->SetStopBits(IUart::stop_1bit);
		    usart["CON3"]->Open(IUart::Auto);
			
	
			usart["CON10"]->SetSPI(spi);
			usart["CON10"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON10"]->SetReceiveLen(8);
			usart["CON10"]->SetReceiveTimeout(65530); //60S
			usart["CON10"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON10"]->SetParityMode(IUart::pm_none);
			usart["CON10"]->SetDataBits(IUart::data_8bits);
			usart["CON10"]->SetStopBits(IUart::stop_1bit);
			usart["CON10"]->SetSlaveOperation(true); //設定Slave 當外部packetage會直接接收存放置register
		    usart["CON10"]->Open(IUart::Auto);
			
			

            static float rtn = 0;				
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

            int time_idx=0; 			
			unsigned long long rs232_trans_cnt=0;
			unsigned long long rs485_trans_cnt=0;
			
			int Receive_cnt=0;
			
			char rs232_txbuf[8]={0xc1,0xFF,0,0,0,0,0,0};
			char rs232_rxbuf[8]={0};
			char DValid_rs232=0;		
		    Upack_t rs232;
				
				
			char rs485_txbuf[8]={0xc1,0xFF,0,0,0,0,0,0};
			char rs485_rxbuf[8]={0};
			char DValid_rs485=1;		
		    Upack_t rs485;
			
			rs232.lastPN=-1;
			rs485.lastPN=0x1;
					
			spi->Update();		
			util::SleepMSTime(16);		
			spi->Update();				
			util::SleepMSTime(16);
			
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                   d0 = util::GetTicks(); 

				   
				   auto rs485sat=usart["CON3"]->GetState();
					if(rs485sat==IMCUUart::UState::Error)
					std::cout<<"RS485 ERROR RS485 ERROR RS485 ERROR"<<std::endl;
					
				   if(rs485sat==IMCUUart::UState::DValid  && DValid_rs485==1)	
					{					
					  DValid_rs485=0;
					  rs485_txbuf[0] = rs485.startflg;
					  rs485_txbuf[1] = rs485.lastPN;
					  rs485_txbuf[1] |= (rs485.rstFlag) ? 0x80 : 0;
					  
					  usart["CON3"]->Send(rs485_txbuf,txlen);
					  
		
					  
					}
															
					std::cout<<" Rs485 Transfer count: "<<rs485_trans_cnt<<std::endl;										
                    spi->Update();										
					usart["CON3"]->Receive(rs485_rxbuf,rxlen);					
					if(rs485_rxbuf[0]==0xc1)
					{
						int recvPN= rs485_rxbuf[1]& 0x7f;
						if( recvPN == rs485.lastPN )
						{
							rs485.rstFlag=0;
							
							DValid_rs485=1;
							rs485.lastPN++;
							if(rs485.lastPN > 127)
							rs485.lastPN=1;
							rs485_trans_cnt++;						
							
						}
		
					}	
                }
				
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 100) //0.1s
                {
                    d2=util::GetTicks();																	
					/***mcu uart **/					
					int rlen=usart["CON10"]->Receive(rs232_rxbuf,rxlen);					
					if(rs232_rxbuf[0]==0xc0)
					{
						int recvPN= rs232_rxbuf[1]& 0x7f;
						if( (recvPN!=0 && rs232.lastPN == -1)  || 
							(recvPN!=0 && (recvPN == rs232.lastPN+1)) ||							
							(recvPN == 1 && (rs232.lastPN == 127))  )				
						{
							rs232.lastPN=recvPN;
														
							//  送出
							rs232_txbuf[0] = 0xc1;
							rs232_txbuf[1] = rs232.lastPN;
							usart["CON10"]->Send(rs232_txbuf,8);
						}
					}
											
				}				
				
            }
            exit(EXIT_SUCCESS);
        }
		
		

		static void dragonball_uart_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");		
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);


			std::map<std::string,IMCUUart*> usart = srm->New_ProjMCU_uart("DragonBallRace");	
			usart["CON10"]->SetSPI(spi);
			usart["CON10"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON10"]->SetReceiveLen(8);
			usart["CON10"]->SetReceiveTimeout(10000); //10s
			usart["CON10"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON10"]->SetParityMode(IUart::pm_none);
			usart["CON10"]->SetDataBits(IUart::data_8bits);
			usart["CON10"]->SetStopBits(IUart::stop_1bit);
		    usart["CON10"]->Open(IUart::Auto);
			
			
			usart["CON3"]->SetSPI(spi);
			usart["CON3"]->SetWorkMode(IMCUUart::WorkMode::passing);
			usart["CON3"]->SetReceiveLen(8);
			usart["CON3"]->SetReceiveTimeout(10000); //10s
			usart["CON3"]->SetBaudRate(IUart::BAUDRATE_115200);
			usart["CON3"]->SetParityMode(IUart::pm_none);
			usart["CON3"]->SetDataBits(IUart::data_8bits);
			usart["CON3"]->SetStopBits(IUart::stop_1bit);
		    usart["CON3"]->Open(IUart::Auto);
					
		

            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;
			int trans_cnt=0;
			int trans_cnt1=0;

			int usart_idx=0;
			char usart_txbuf[40]={0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48};
			char usart_rxbuf[40]={0};
					
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();									
                }
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
			
						
						auto resat=usart["CON10"]->GetState();
						if(resat==IMCUUart::UState::Error)
						 std::cout<<"USART ERROR"<<std::endl;
						
						
						if(resat==IMCUUart::UState::DValid)	
						{
							int rlen=usart["CON10"]->Receive(usart_rxbuf,rxlen);
							usart["CON10"]->Send(usart_txbuf,txlen);
							
							std::cout<<"USART usart_rxbuf count:"<<trans_cnt<<" ";
							for(auto& each:usart_rxbuf)
								std::cout << each<<"  "; 
							std::cout<< std::endl;
							
							std::cout<<"USART usart_txbuf count:"<<trans_cnt<<" ";
							for(auto& each:usart_txbuf)
								std::cout << each<<"  "; 
							std::cout<< std::endl;
							

							trans_cnt++;
						}
						else if(resat==IMCUUart::UState::Transferring)	
							std::cout<<"USART Transferring count: "<<trans_cnt<<std::endl;
					
						
						auto resat1=usart["CON3"]->GetState();
						if(resat1==IMCUUart::UState::Error)
						 std::cout<<"USART ERROR"<<std::endl;
						
						
						if(resat1==IMCUUart::UState::DValid)	
						{
							int rlen=usart["CON3"]->Receive(usart_rxbuf,rxlen);
							usart["CON3"]->Send(usart_txbuf,txlen);
							
							std::cout<<"USART usart_rxbuf count:"<<trans_cnt1<<" ";
							for(auto& each:usart_rxbuf)
								std::cout << each<<"  "; 
							std::cout<< std::endl;
							
							std::cout<<"USART usart_txbuf count:"<<trans_cnt1<<" ";
							for(auto& each:usart_txbuf)
								std::cout << each<<"  "; 
							std::cout<< std::endl;
		
							trans_cnt1++;
						}
						else if(resat1==IMCUUart::UState::Transferring)	
							std::cout<<"USART Transferring count: "<<trans_cnt1<<std::endl;
						

									
                }
            }

            exit(EXIT_SUCCESS);
        }
				
		 static void dragonball_gpio_demo()
        {
            ISerialManager* srm = GetSerialManagerInstance();
            //ISpi* spi = srm->NewM01Spi();	
            ISpi* spi = srm->New_ProjMCU_spi("DragonBallRace");
            spi->SetWorkingFrequency(10000000);
            bool open_result = spi->Open(ISpi::cs0);


			
			IGPIOManager* gpm = GetGPIOManagerInstance();
			std::vector<IGPIO*> gpin;
			std::vector<IGPIO*> gpout;
			for(int i=0;i<15;i++) //依據專案定義GPIO output
			{
				IGPIO* gptmp = gpm->New_ProjMCU("DragonBallRace");
				gptmp->SetSPI(spi);
				gptmp->SetMode(IGPIO::mode::output);
				gptmp->SetNumber(i);
				gpout.push_back(gptmp);			
			}
			
			for(int i=0;i<8;i++) //依據專案定義GPIO input
			{
				IGPIO* gptmp = gpm->New_ProjMCU("DragonBallRace");
				gptmp->SetSPI(spi);
				gptmp->SetMode(IGPIO::mode::input);
				gptmp->SetNumber(i);
				gpin.push_back(gptmp);			
			}
			
		

            static float rtn = 0;				
            int CoinVal=0;
            int TicketVal=0;
            unsigned long long delta;
            static unsigned long long d0 = util::GetTicks();
            static unsigned long long d1 = util::GetTicks();
            static unsigned long long d2 = util::GetTicks();
            static unsigned long long d3 = util::GetTicks();

			int lamptime_idx=0; 
            int time_idx=0; 			
            int init_flg=0;
            unsigned char memory=0;
            int act_cnt=0;



			int usart_idx=0;
			char usart_txbuf[40]={0x41};
			char usart_rxbuf[40]={0};
					
            while(1)
            {
                d1 = util::GetTicks();
                delta= d1-d0;
                if (delta >= 16)
                {
                    d0 = util::GetTicks();
                    spi->Update();									
					  for(auto each:gpin)
                    {					

                        if(each->GetState() == IGPIO::state::high)
                            std::cout<<"Input"<< each->GetNumber()<<": High  ";
                        else
                            std::cout<<"Input"<< each->GetNumber()<<": Low   ";		
                    }
                    std::cout<<std::endl;	
                }
                d3 = util::GetTicks();
                delta= d3-d2;
                if (delta >= 500) //0.5s printf
                {
                    d2=util::GetTicks();
                    time_idx++;
				 					 
					if(time_idx==4)
					{
						act_cnt=0;
						for(auto each:gpout)
						{					
							if(act_cnt%2)
							each->SetState(IGPIO::state::low);
							else
							each->SetState(IGPIO::state::high);
							
							act_cnt++;
						}											
					}
					else if(time_idx==8)
					{
						act_cnt=0;		
						for(auto each:gpout)
						{					
							if(act_cnt%2)
							each->SetState(IGPIO::state::high);
							else
							each->SetState(IGPIO::state::low);							
							act_cnt++;
						}
													
						time_idx=0;
					}
									
                }
            }

            exit(EXIT_SUCCESS);
        }
		
		
    };
};
